/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom;

import java.io.Serializable;
import net.jcip.annotations.Immutable;

/**
 * Represent the address of an {@link Actor}, returned by {@link Topology} instances at spawning time and used to access the actor itself.<br>
 * This class implements the composite pattern, representing both a local and a remote address, depending on how it has been created.
 * <br><br>
 * A <strong>local</strong> address is composed by a symbolic host name, representing a symbolic host for the
 * local topology, a static port and the actor identifier (see {@link com.googlecode.actorom.local.LocalTopology}).
 * <br><br>
 * A <strong>remote</strong> address is composed by an actual host name and port, representing the host and port
 * the server topology is bound to, and the actor identifier
 * (see {@link com.googlecode.actorom.remote.ClientTopology} and {@link com.googlecode.actorom.remote.ServerTopology}).
 * 
 * @author Sergio Bossa
 */
@Immutable
public class Address implements Serializable {

    private static final long serialVersionUID = 12345678901L;
    private static final String SEPARATOR = ":";
    private static final String SYMBOLIC_HOST_PREFIX = "vm" + SEPARATOR;
    private static final int SYMBOLIC_PORT = 0;
    private final String host;
    private final int port;
    private final String id;
    private final String address;

    /**
     * Create a new address referring to a local actor in a local topology.
     *
     * @param symbolicHost The symbolic host name of the local topology the address refers to.
     * @param id The actor identifier.
     * @return A local address.
     */
    public static Address newLocalAddress(String symbolicHost, String id) {
        return new Address(symbolicHost, id);
    }

    /**
     * Create a new address referring to a remote actor in a server topology.
     *
     * @param host The remote host.
     * @param port The remote port.
     * @param id The actor identifier.
     * @return A remote address.
     */
    public static Address newRemoteAddress(String host, int port, String id) {
        return new Address(host, port, id);
    }

    private Address(String symbolicHost, String id) {
        this.host = SYMBOLIC_HOST_PREFIX + symbolicHost;
        this.port = SYMBOLIC_PORT;
        this.id = id;
        this.address = host + SEPARATOR + port + SEPARATOR + id;
    }

    private Address(String host, int port, String id) {
        this.host = host;
        this.port = port;
        this.id = id;
        this.address = host + SEPARATOR + port + SEPARATOR + id;
    }

    /**
     * Get the actor's topology host this address refers to.
     *
     * @return The host, which may be a symbolic host if this is a local address
     * referring to a local, in-memory, actor, or an actual host name if this is a remote address.
     */
    public String getHost() {
        return host;
    }

    /**
     * Get the actor port this address refers to.
     *
     * @return The port.
     */
    public int getPort() {
        return port;
    }

    /**
     * Get the actor identifier this address refers to.
     *
     * @return The actor identifier.
     */
    public String getId() {
        return id;
    }

    /**
     * Get the address value.
     *
     * @return The address value.
     */
    public String getAddress() {
        return address;
    }

    /**
     * Check whether this address is a local one.
     *  
     * @return True if local, false otherwise.
     */
    public boolean isLocal() {
        return this.host.startsWith(SYMBOLIC_HOST_PREFIX) && this.port == SYMBOLIC_PORT;
    }

    /**
     * Check whether this address is a remote one.
     *
     * @return True if remote, false otherwise.
     */
    public boolean isRemote() {
        return !this.host.startsWith(SYMBOLIC_HOST_PREFIX) && this.port != SYMBOLIC_PORT;
    }

    /**
     * Check whether this address is a neighbor of the given address.<br>
     * Two addresses are neighbors if referring to actors on the same topology.
     *
     * @param other The address to compare.
     * @return True if neighbors, false otherwise.
     */
    public boolean isNeighborOf(Address other) {
        return this.host.equals(other.getHost()) && this.port == other.getPort();
    }

    @Override
    public int hashCode() {
        return address.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Address) {
            Address other = (Address) obj;
            return address.equals(other.address);
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return address.toString();
    }
}
